
app.view.onload = function(result){
	if (result.error)
	{
		$("#tip").hide();
		$("#auth_box").css({"display": "table-cell"});
		$("#weibo_auth_page").attr({"src": result.auth_url});
		
		return;
	}

	$("#user_name").text(result.user.name);
	$("#user_icon").css({"background-image": "url("+result.user.icon+")"});

	var webos_html = [];
	for (var i = 0; i < result.weibos.length; ++i)
	{
		var weibo = result.weibos[i];
		webos_html.push("<div class=\"tweet\"><div class=\"meta\"><div class=\"poster_icon\"><img src=\""+ weibo.poster_icon +"\"/></div><div class=\"post_detail\"><div class=\"poster_name\">" + weibo.poster_name + "</div><div class=\"post_date\">" + new Date(weibo.post_date).toLocaleString() + "</div></div></div><div class=\"content\">" + weibo.content + "</div></div>");
	}

	$("#weibo_list").html(webos_html.join(""));
	$("#load").fadeOut();

	var wbox = $("#weibos_box");
	// Firefox		
	wbox.on("DOMMouseScroll", function(evt){
		var delta =  evt.originalEvent.detail > 0 ? 20 : -20;
		scoll_weibolist(delta);
	});
	// IE/Opera/Chrome
	wbox.on("mousewheel", function(evt){
		var delta =  evt.originalEvent.wheelDelta < 0 ? 20 : -20;
		scoll_weibolist(delta);
	});
	
	function scoll_weibolist(delta){
		var new_top = $("#weibo_list").position().top - delta;
		$("#weibo_list").css({"top": new_top + "px"});
		
		var rest_height = $("#weibo_list").height() + new_top - ($(".main").height() - $(".top").height());
		// console.log("rest height: %d", rest_height);
		if (rest_height < 20)
		{
			app.loadmore();
		}
	};
};

app.view.onloadmore = function(result){
	if (result.error)
	{
		alert(result.error);
	}
	else
	{
		var webos_html = [];
		for (var i = 0; i < result.weibos.length; ++i)
		{
			var weibo = result.weibos[i];
			webos_html.push("<div class=\"tweet\"><div class=\"meta\"><div class=\"poster_icon\"><img src=\""+ weibo.poster_icon +"\"/></div><div class=\"post_detail\"><div class=\"poster_name\">" + weibo.poster + "</div><div class=\"post_date\">" + weibo.post_date + "</div></div></div><div class=\"content\">" + weibo.content + "</div></div>");
		}

		$("#weibo_list").append(webos_html.join(""));
	}
};

app.view.onrefresh = function(result){
	if (result.error)
	{
		alert(result.error);
	}
	else
	{
		if (result.weibos.length === 0)
		{
			return;
		}
		
		var webos_html = [];
		for (var i = 0; i < result.weibos.length; ++i)
		{
			var weibo = result.weibos[i];
			webos_html.push("<div class=\"tweet\"><div class=\"meta\"><div class=\"poster_icon\"><img src=\""+ weibo.poster_icon +"\"/></div><div class=\"post_detail\"><div class=\"poster_name\">" + weibo.poster + "</div><div class=\"post_date\">" + weibo.post_date + "</div></div></div><div class=\"content\">" + weibo.content + "</div></div>");
		}

		$("#weibo_list").prepend(webos_html.join(""));
	}
};

app.view.init = function(){
	$("#side_bar_button").on("click", function(){
		$("#side_bar_mask").fadeIn(250);
		$("#side_bar").css({'left': '-250px'});
		$("#side_bar").animate({
			left: "0px"
		}, 250);
	});

	$("#side_bar_mask").on("click", function(){
		$("#side_bar").animate({
			left: "-250px"
		}, 250);
		$(this).fadeOut(250);
	});
	
	$("#new_tweet").on("click", function(){
		$("#new_tweet_box_mask").fadeIn();
		$(".new_tweet_box").fadeIn();
	});
	$("#new_tweet_box_mask").on("click", function(){
		$(this).fadeOut();
	});
};
